// pages/partyDetail/index.js
var time= require("../../utils/util.js");
const db=wx.cloud.database();
const _ = db.command
var app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    reply:'',
    loading: true,
    rpValue:"发表想法",//回复框中的文字
    id:'',
    userid:'',
    head_pic:'',
    current_item:'{}',
    data_list:[],
    haveimge:false,
    ismsgR:true,//文字违规
    _openid: '',
    location:''
  },
  async goToChartRoom(e){
    console.log(e.currentTarget.dataset.id);
    let uID = e.currentTarget.dataset.id;
    let name = e.currentTarget.dataset.name;
    let MyID = wx.getStorageSync('userInfo')._id;
    let res = await db.collection('user').doc(uID).get();
    let uTime = res.data.timestamp;
    let mTime = wx.getStorageSync('userInfo').timestamp;
    let gid = uTime+mTime;
   let id = uID + "" + MyID;
    wx.navigateTo({
      url: `/pages/im/room/room?id=${id}&name=${name?name:this.data.uName}&gid=${gid}`,
    });
  },
  cancelReply(){
    this.setData({//变量变为初始值
      rpValue:'发表想法',
      current_item:'{}'
    })
  },
  Replycomment(e){
    var tmp =e.currentTarget.dataset
    this.setData({
      current_item:tmp.detail_info,
    })
    console.log(this.data.current_item)
    this.setData({
      rpValue:'回复 '+": "+this.data.current_item.pContent
    })
  },
  getData(num=6,page=0,isconcat=true){
    wx.cloud.callFunction({
      name:"getPartyDetail",
      data:{
        num:num,
        page:page,
        partyID:this.data.id
      }
    }).then(res=>{
      var list=res.result.data
      var list_size=list.length
      for(var i=0;i<list_size;i++){
        var item_time=time.formatTime(list[i].pTime)
        list[i].time=item_time
      }
      if(isconcat==true){
        var oldData=this.data.data_list
        var newData=oldData.concat(list)
      }
      else{
        var newData=list
      }
      this.setData({
        data_list:newData
      })
    })
    const that = this;
      setTimeout(()=>{
        that.setData({
          loading:false
        })
      },300)
  },
  inputReply:function(e){
    var reply=e.detail.value
    this.setData({
      reply:reply
    })
  },
  pulishReply:function(e){

    var that=this
    if(this.data.reply.length==0){
      wx.showToast({
        icon:'none',
        title: '回复内容',
      })
    }
    else{
      wx.showLoading({
        title: '发布中',
        mask:true
      })
      var data = {
        partyID:that.data.id,
        pContent:that.data.reply,
        pTime: Date.parse(new Date())/1000,
        uName:app.globalData.uName,
        head_pic:app.globalData.head_pic,
        pReplyContent:this.data.current_item=='{}'? '':this.data.current_item.pContent,
        pReplyUserName:this.data.current_item=='{}'? '':this.data.current_item.uName,
      }

      wx.cloud.callFunction({
        name: 'contentCheck',
        data: {
          msg: that.data.reply
        },
        success(res) {
          if (res.result.errCode == 87014) {
            console.log(res.errCode)
            wx.showToast({
              icon: 'none',
              title: '文字违规',
            })
          }
          else{
            console.log(that.data.id);
            db.collection("partysInfo").doc(that.data.id)
            .update({
              data:{
                'lastReplyTime':Date.parse(new Date())/1000
              }
            }).then(res=>{
              console.log(res);
            })
            db.collection("replyOfParty").add({
              data:data,
              success:res=>{
             
                wx.hideLoading()
                wx.showToast({
                  title: '发布成功',
                })
              },
              fail:e=>{
                wx.showToast({
                  title: '发布错误',
                })
                console.log(e)
              },
              complete: () => {
                that.getData(6,0,false)
                that.setData({//发布完了就把回复框中的置为初始值
                    reply:'',
                    rpValue:'发表想法',
                    current_item:'{}'
                  })
                }
              })
          }
        }
      })
    
      }
 } ,
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    db.collection("partysInfo").doc(options.id.replace(/\s+/g,""))
    .get()
    .then(res=>{
      console.log(res.data);
      var partyInfo=res.data
      this.setData({
        uName:partyInfo.oName,
        id:partyInfo._id,
        head_pic:partyInfo.head_pic,
        partyTopic:partyInfo.partyTopic,
        imgPaths:partyInfo.imgPaths,
        pTime:time.formatTime(partyInfo.pTime),
        _openid:partyInfo.partyOwner,
        content:partyInfo.partyContent,
        overTime:time.formatTime_overtime(partyInfo.pOverTime+partyInfo.pTime)
      })

      this.getData()
      // console.log(this.data);
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onLoad()

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var page=this.data.data_list.length
    this.getData(7,page,true)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  joinParty(){
    console.log("尝试调用"),
    wx.cloud.callFunction({
      name:"joinParty",
      success:res=>{
        console.log(res);
      }
    })
  }
})